package com.leo.component;

import com.almasb.fxgl.entity.component.Component;

import static com.leo.common.Config.TANK_MOVE_SPEED;

public class TankComponent extends Component {

    /**
     * 当前帧是否在移动
     */
    private boolean movedThisFrame = false;

    private double distance;

    @Override
    public void onAdded() {

    }

    /**
     * tpf 代表这一帧的用时；一般来说，60帧每一秒
     * <p>
     * 移动的距离是一个变量 时间 * 速度 = 距离
     *
     * @param tpf time per frame
     */
    @Override
    public void onUpdate(double tpf) {
        movedThisFrame = false;
        distance = tpf * TANK_MOVE_SPEED;
    }

    public void moveUp() {
        if (movedThisFrame) {
            return;
        }
        movedThisFrame = true;
        entity.setRotation(0);
        entity.translate(0, -distance);
    }

    public void moveDown() {
        if (movedThisFrame) {
            return;
        }
        movedThisFrame = true;
        entity.setRotation(180);
        entity.translate(0, distance);
    }

    public void moveLeft() {
        if (movedThisFrame) {
            return;
        }
        movedThisFrame = true;
        entity.setRotation(270);
        entity.translate(-distance, 0);
    }

    public void moveRight() {
        if (movedThisFrame) {
            return;
        }
        movedThisFrame = true;
        entity.setRotation(90);
        entity.translate(distance, 0);
    }

    public void shoot() {

    }
}
